/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.common;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Thinh Hung
 */
public class ConfigUtil {

    private final String CONFIG_FILE = "config.xml"; // config file name
    private Element elRoot; //root element
    
    // Configuration tags of Database Server;
    public static final String DB_HOST = "db-host";
    public static final String DB_PORT = "db-port";
    public static final String DB_NAME = "db-name";
    public static final String DB_USERNAME = "db-username";
    public static final String DB_PASSWORD = "db-password";

    // Configuration tag: number of items per a page
    public static final String ITEMS_PER_PAGE = "items-per-page";
    
    // Configuration tags of Outgoing Mail (SMTP) Server.;
    public static final String SMTP_HOST = "smtp-host";
    public static final String SMTP_AUTH = "smtp-auth";
    public static final String SMTP_TYPE = "smtp-type";
    public static final String SMTP_PORT = "smtp-port";
    public static final String SMTP_USERNAME = "smtp-username";
    public static final String SMTP_PASSWORD = "smtp-password";
    public static final String SUPPORT_EMAIL = "support-email";
    public static final String SUPPORT_NAME = "support-name";

    public ConfigUtil() {
        try {
            // get the file path
            URL url = ConfigUtil.class.getResource("ConfigUtil.class");
            String className = url.getFile().replace("%20", " ");
            String filePath = className.substring(0, className.indexOf("WEB-INF") + "WEB-INF".length());
            filePath += "\\" + CONFIG_FILE;
            
            // read the file
            File fXmlFile = new File(filePath);
            
            // parse the xml file
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            elRoot = doc.getDocumentElement();
        } catch (SAXException ex) {
            Logger.getLogger(ConfigUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ConfigUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(ConfigUtil.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    // get config item
    public String getConfig(String configName) {

        NodeList nlList = elRoot.getElementsByTagName(configName).item(0).getChildNodes();

        Node nValue = (Node) nlList.item(0);

        return nValue.getNodeValue();
    }
}
